#!/usr/bin/env python
#
#       browser.py
#       
#       Copyright 2009 Matthew Brush <mbrush AT leftclick DOT ca>
#       
#       This program is free software; you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation; either version 2 of the License, or
#       (at your option) any later version.
#       
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU General Public License for more details.
#       
#       You should have received a copy of the GNU General Public License
#       along with this program; if not, write to the Free Software
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#       MA 02110-1301, USA.
#
import sys
import gtk
import browser
import editor
import terminal

class MainWindow:
 
    def on_window_main_destroy(self, widget, data=None):
        gtk.main_quit()
    
    # insert an Editor component at the first position and select it
    def _add_new_doc(self):
        default_doc = editor.Editor()
        self._notebook_open_files.prepend_page(default_doc, 
            gtk.Label("Untitled"))
        default_doc.show()
        self._notebook_open_files.set_current_page(0)
    
    def _open_doc(self):
        filechooser = gtk.FileChooserDialog(
            title="Open File", 
            parent=None,
            action=gtk.FILE_CHOOSER_ACTION_OPEN,
            buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_OK, gtk.RESPONSE_OK))
        result = filechooser.run()
        if result == gtk.RESPONSE_OK:
            print "Ok chosen in file chooser."
            print "Open not implemented."
        else:
            print "Cancel chosen in file chooser."
        filechooser.destroy()
        
                
    def _save_doc_as(self, editor):
        filechooser = gtk.FileChooserDialog(
            title="Save As", 
            parent=None,
            action=gtk.FILE_CHOOSER_ACTION_SAVE,
            buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_OK, gtk.RESPONSE_OK))
        result = filechooser.run()
        if result == gtk.RESPONSE_OK:
            print "Ok chosen in file chooser."
        else:
            print "Cancel chosen in file chooser."
        filechooser.destroy()
    
    def _save_doc(self, editor):
        if editor.ever_saved == False:
            self._save_doc_as(editor)

    def on_toolbutton_open_clicked(self, widget, data=None):
        self._open_doc()

    def on_toolbutton_save_clicked(self, widget, data=None):
        current_page = self._notebook_open_files.get_current_page()
        current_editor = self._notebook_open_files.get_nth_page(current_page)
        self._save_doc(current_editor)
        
    def on_toolbutton_new_clicked(self, widget, data=None):
        self._add_new_doc()
	
    def __init__(self):
        
        self.builder = gtk.Builder()
        self.builder.add_from_file("codescribe.glade")

        self.window = self.builder.get_object("window_main")
        self._notebook_bottom_utils = self.builder.get_object("notebook_bottom_utils")
        self._notebook_open_files = self.builder.get_object("notebook_open_files")
        self._hpaned_main = self.builder.get_object("hpaned_main")

        # Remove placeholder tabs
        self._notebook_open_files.remove_page(0)
        self._notebook_bottom_utils.remove_page(0)
                
        # Global Browser
        self._global_browser = browser.WebBrowserBox(toolbar=True, 
            statusbar=True, addr="http://www.google.com")
        self._notebook_open_files.append_page(self._global_browser, gtk.Label("Global Browser"))
        self._global_browser.show()
        
        # First Untitled Document
        self._add_new_doc()
        
        # Terminal tab in bottom area
        self._terminal = terminal.Terminal()
        self._notebook_bottom_utils.append_page(self._terminal, gtk.Label("Terminal"))
        self._terminal.show()
                
        self.builder.connect_signals(self)      
   
if __name__ == "__main__":
	win = MainWindow()
	gtk.main()
